package com.atung.provider;

import java.util.Objects;

import org.apache.ibatis.jdbc.SQL;

import com.atung.model.BusCompanion;

public class BusCompanionProvider {

	public String insterCompanion(BusCompanion bCompanion){
		
		return new SQL(){
			{
				INSERT_INTO("bus_companion");
				VALUES("id", "#{id}");
				VALUES("open_id", "#{openId}");
				VALUES("companion_name", "#{companionName}");
				VALUES("companion_phone", "#{companionPhone}");
				if(Objects.nonNull(bCompanion.getIsDelect())){
					VALUES("is_delect", "#{isDelect}");
				}
				VALUES("creation_time", "#{creationTime}");
				VALUES("modification_time", "#{modificationTime}");
				
			}
		}.toString();	
	}
	
	public String updateCompanion(BusCompanion bCompanion){
		
		return new SQL(){
			{
				UPDATE("bus_companion");
				if(Objects.nonNull(bCompanion.getCompanionName())&&!Objects.equals(bCompanion.getCompanionName(), "")){
					SET("companion_name=#{companionName}");
				}if(Objects.nonNull(bCompanion.getCompanionPhone())&&!Objects.equals(bCompanion.getCompanionPhone(),"")){
					SET("companion_phone=#{companionPhone}");
				}
				SET("modification_time=#{modificationTime}");
				WHERE("id=#{id}  and is_delect=0  and open_id=#{openId}");										
			}
		}.toString();	
	}
}
